{% import "macros/user_name.html.j2" as USER_NAME %}

{% macro TAG(
      field,
      field_kind,
      project_manager,
      value_types,
      user_id)
-%}

  {% if value_types.string == field_kind.value_type %}
    {# Строка #}
    {{ field.value|e }}

  {% elif value_types.markdown_text == field_kind.value_type %}
    {# Текст markdown #}
    <div id="field_kind_{{ field_kind.id }}_view"></div>

  {% elif value_types.integer == field_kind.value_type %}
    {# Целое число #}
    {{ field.value }}

  {% elif value_types.float == field_kind.value_type %}
    {# Дробное число #}
    {{ field.value }}

  {% elif value_types.bool == field_kind.value_type %}
    {# Флаг #}
    {% if field.value %}On{% else %}Off{% endif %}

  {% elif value_types.date_time == field_kind.value_type %}
    {# Дата/время #}
    {{ field.value }}

  {% elif value_types.uri == field_kind.value_type %}
    {# Ссылка #}
    <a href="{{ field.value|e }}">{{ field.value|e }}</a>

  {% elif value_types.select == field_kind.value_type %}
    {# Выпадающий список (единичный) #}
    {% if not field.value == '0' %}
      {{ field.value|e }}
    {% endif %}

  {% elif value_types.select_multi == field_kind.value_type %}
    {# Выпадающий список (множественный) #}
    {% for value in field.value_as_list() %}
      {% if not value == '0' %}
        {{- value|e -}}
        {%- if not loop.last %}, {% endif -%}
      {% endif %}
    {% endfor %}

  {% elif value_types.issue_id == field_kind.value_type %}
    {# Запрос (issue) #}
    {% set sub_issue = project_manager.issue(field.value) %}
    {% if sub_issue %}
      <a href="/issues/issue/{{ sub_issue.id }}">
        {{ project_manager.issue_kind(sub_issue.issue_kind_id).caption|e }}
        #{{ sub_issue.id }}: {{ sub_issue.caption|e }}
      </a>
    {% endif %}

  {% elif value_types.user_id == field_kind.value_type %}
    {# Пользователь #}
    {% set target_user = project_manager.user_by_id(field.value) %}
    {% if target_user %}
      <a  href="/users/user_statistics/{{ target_user.id }}"
          class="d-flex align-items-center">
        {{ USER_NAME.VALUE_WITH_ICON(target_user, user_id) }}
      </a>
    {% endif %}

  {% elif value_types.project_id == field_kind.value_type %}
    {# Проект #}
    {% set project = project_manager.project(field.value) %}
    {% if project %}
      <a href="/projects/project/{{ project.id }}" class="d-flex align-items-center">
        <span class="badge rounded-pill bg-secondary mr-5">{{ project.id }}</span>
        {{ project.caption|e }}
      </a>
    {% endif %}

  {% elif value_types.version_id == field_kind.value_type %}
    {# Версия #}
    {% set version = project_manager.version(field.value) %}
    {% if version %}
      <a href="/projects/version/{{ version.id }}" class="d-flex align-items-center">
        <span class="badge rounded-pill bg-secondary mr-5">{{ version.id }}</span>
        {{ version.caption|e }}
      </a>
    {% endif %}

  {% elif value_types.issues_ids == field_kind.value_type %}
    {# Перечень запросов (issue) #}
    {% for id in field.value_as_ids() %}
      {% set sub_issue = project_manager.issue(id) %}
      {% if sub_issue %}
        <a href="/issues/issue/{{ sub_issue.id }}">
          {{ project_manager.issue_kind(sub_issue.issue_kind_id).caption }}
          #{{ sub_issue.id }}: {{ sub_issue.caption|e }}
        </a>
      {% endif %}
    {% endfor %}

  {% elif value_types.users_ids == field_kind.value_type %}
    {# Перечень пользователей #}
    {% for id in field.value_as_ids() %}
      {% set target_user = project_manager.user(id) %}
      {% if target_user %}
        <a href="/users/user_statistics/{{ target_user.id }}" class="d-flex align-items-center">
          {{ USER_NAME.VALUE_WITH_ICON(target_user, user_id) }}
        </a>
      {% endif %}
    {% endfor %}

  {% elif value_types.projects_ids == field_kind.value_type %}
    {# Перечень проектов #}
    {% for id in field.value_as_ids() %}
      {% set project = project_manager.project(id) %}
      {% if project %}
        <a href="/projects/project/{{ project.id }}">
          <span class="badge rounded-pill bg-secondary">{{ project.id }}</span>
          {{ project.caption|e }}
        </a>
        {% if not loop.last %}
          ,
        {% endif %}
      {% endif %}
    {% endfor %}

  {% elif value_types.versions_ids == field_kind.value_type %}
    {# Перечень версий #}
    {% for id in field.value_as_ids() %}
      {% set version = project_manager.version(id) %}
      {% if version %}
        <a href="/projects/version/{{ version.id }}">
          <span class="badge rounded-pill bg-secondary">{{ version.id }}</span>
          {{ version.caption|e }}
        </a>
        {% if not loop.last %}
          ,
        {% endif %}
      {% endif %}
    {% endfor %}

  {% else %}
    {{ field.value }}
  {% endif %}

{%- endmacro %}
